package BinaryTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Offer34pathSum {
    List<List<Integer>> res= new ArrayList<>();
    List<Integer> list=new ArrayList<>();
    int sum=0;
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        dfs(root,target);
        return res;
    }

    private void dfs(TreeNode root, int target) {
        if(root==null){
            return;
        }
        sum+=root.val;
        list.add(root.val);
        if(root.left==null && root.right==null){
            if(sum==target){
                res.add(new ArrayList<>(list));
            }
        }
        dfs(root.left,target);
        dfs(root.right,target);
        sum-=root.val;
        list.remove(list.size()-1);
    }
}
